﻿using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Geometry;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 地理信息系统开发教程
{
    public partial class 几何元素示例 : Form
    {
        private bool bIsPointMenueClicked = false;
        private bool bIsLineMenueClicked = false;
        private bool bIsPolygonMenueClicked = false;
        private bool bIsSelectedElementClicked = false;
        IMap pMap;
        IActiveView pActiveView;
        IGraphicsContainer pGraphicsContainer;
        IElement pElement;
        IEnvelope pEnvelope;
        IEnumElement pEnumElement;
        IGraphicsContainerSelect pGraphicsContainerSelect;

        public 几何元素示例()
        {
            InitializeComponent();
        }

        private void 几何要素示例_Load(object sender, EventArgs e)
        {
            pMap = axMapControl1.Map;
            pActiveView = axMapControl1.ActiveView;
            pGraphicsContainer = pMap as IGraphicsContainer;
        }

        #region 标识添加点线面
        //添加点
        private void addPoint_Click(object sender, EventArgs e)
        {
            bIsPointMenueClicked = true;
            bIsLineMenueClicked = false;
            bIsPolygonMenueClicked = false;
        }
        //添加线
        private void addLine_Click(object sender, EventArgs e)
        {
            bIsPointMenueClicked = false;
            bIsLineMenueClicked = true;
            bIsPolygonMenueClicked = false;
        }
        //添加面
        private void addPolygon_Click(object sender, EventArgs e)
        {
            bIsPointMenueClicked = false;
            bIsLineMenueClicked = false;
            bIsPolygonMenueClicked = true;
        }
        //选择要素
        private void button2_Click(object sender, EventArgs e)
        {
            bIsPointMenueClicked = false;
            bIsLineMenueClicked = false;
            bIsPolygonMenueClicked = false;
            bIsSelectedElementClicked = true;
        }
        #endregion

        #region Mapcontrol事件
        private void axMapControl1_OnMouseDown(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnMouseDownEvent e)
        {
            if (e.button == 1)
            {
                #region 绘制点
                if (bIsPointMenueClicked)
                {
                    //新建点
                    IPoint pPoint = new PointClass();
                    pPoint.PutCoords(e.mapX, e.mapY);
                    //产生一个Marker元素
                    IMarkerElement pMarkElement = new MarkerElementClass();
                    //产生一个修饰Marker元素的symbol
                    ISimpleMarkerSymbol pMarkerSymbol = new SimpleMarkerSymbolClass();
                    //调用定义颜色的方法设置符号的颜色
                    pMarkerSymbol.Color = GetRGB(220, 120, 60);

                    //设置符号的大小
                    pMarkerSymbol.Size = 2;

                    //设置符号类型
                    pMarkerSymbol.Style = esriSimpleMarkerStyle.esriSMSDiamond;
                    pElement = pMarkElement as IElement;

                    //得到Element的接口对象，用于设置元素的Geometry
                    pElement.Geometry = pPoint;
                    pMarkElement.Symbol = pMarkerSymbol;

                    //将元素加入到Map中

                    pGraphicsContainer.AddElement(pMarkElement as IElement, 0);

                    pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);

                }
                #endregion

                #region 绘制线

                if (bIsLineMenueClicked)
                {
                    ILineElement pLineElement = new LineElementClass();
                    ISimpleLineSymbol pSimpleLineSymbol = new SimpleLineSymbolClass();
                    IPolyline pPolyLine = new PolylineClass();
                    IRubberBand pRubberBand = new RubberLineClass();
                    pPolyLine = (IPolyline)pRubberBand.TrackNew(axMapControl1.ActiveView.ScreenDisplay, null);
                    pSimpleLineSymbol.Color = GetRGB(11, 200, 145);
                    pSimpleLineSymbol.Width = 2;
                    pSimpleLineSymbol.Style = esriSimpleLineStyle.esriSLSSolid;

                    IElement pElement = (IElement)pLineElement;
                    pElement.Geometry = pPolyLine;
                    pLineElement.Symbol = pSimpleLineSymbol;
                    pGraphicsContainer = (IGraphicsContainer)axMapControl1.Map;
                    pGraphicsContainer.AddElement((IElement)pLineElement, 0);
                    axMapControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
                }

                #endregion

                #region 绘制面

                if (bIsPolygonMenueClicked)

                {

                    IPolygon pPolygon = axMapControl1.TrackPolygon() as IPolygon;

                    //产生一个SimpleFillSymbol对象

                    ISimpleFillSymbol pFillSymbol = new SimpleFillSymbolClass();

                    pFillSymbol.Style = esriSimpleFillStyle.esriSFSSolid;

                    pFillSymbol.Color = GetRGB(220, 110, 60);

                    //产生一个PolygonElement对象

                    IFillShapeElement pPolygonElement = new PolygonElementClass();

                    pPolygonElement.Symbol = pFillSymbol;

                    pElement = pPolygonElement as IElement;

                    pElement.Geometry = pPolygon;

                    //将元素添加到Map对象中

                    pGraphicsContainer.AddElement(pPolygonElement as IElement, 0);

                    pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);

                }

                #endregion

            }

            if (e.button == 2)
            {
                if (bIsSelectedElementClicked)
                {
                    pEnvelope = axMapControl1.TrackRectangle();
                    pEnumElement = pGraphicsContainer.LocateElementsByEnvelope(pEnvelope);
                    pGraphicsContainerSelect = axMapControl1.Map as IGraphicsContainerSelect;
                    if (pEnumElement != null)
                    {
                        pGraphicsContainerSelect.SelectElements(pEnumElement);
                    }
                    else
                    {
                        pGraphicsContainerSelect.UnselectAllElements();//点击空白处是，取消所有selected。
                    }
                    axMapControl1.ActiveView.Refresh();//可以换个刷新
                }
            }
        }

        private void axMapControl1_OnDoubleClick(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnDoubleClickEvent e)
        {
            bIsPointMenueClicked = false;
            bIsLineMenueClicked = false;
            bIsPolygonMenueClicked = false;
        }
        #endregion

        //删除元素
        private void button1_Click(object sender, EventArgs e)
        {
            pGraphicsContainerSelect = axMapControl1.Map as IGraphicsContainerSelect;
            //遍历Element
            IEnumElement pEnumElemen = pGraphicsContainerSelect.SelectedElements;
            pEnumElemen.Reset();

            IElement pElement;
            while ((pElement = pEnumElemen.Next()) != null)
            {
                pGraphicsContainer.DeleteElement(pElement);
            }
            axMapControl1.ActiveView.Refresh();//可以换个刷新
        }

        //定义颜色的方法
        private IRgbColor GetRGB(int red, int green, int blue)
        {
            IRgbColor pRgbColor = new RgbColorClass();
            pRgbColor.Red = red;
            pRgbColor.Green = green;
            pRgbColor.Blue = blue;
            return pRgbColor;
        }
    }
}
